1 using System;
2 using
System.Collections.Generic;
3 using
System.ComponentModel;
4 using
System.Data;
5 using
System.Drawing;
6 using
System.Linq;
7 using
System.Management;
8 using
System.Text;
9 using
System.Threading.Tasks;
10 using
System.Windows.Forms;
11 using
System.Xml;
12 using
System.Xml.Linq;
13
14 //
using System.Diagnostics;
15 //
using Microsoft.SqlServer.Management.Smo;
16 //
using System.ServiceProcess;
17 //
using Microsoft.SqlServer.Management.Smo.Wmi;
18 //
using Microsoft.SqlServer.Management.Common;
19
20
21 namespace
SoftQuanLyNhaHang.Views
22 {
23     
public partial class frmConnectCSDL : Form
24     {
25         
public frmConnectCSDL()
26         {
27             InitializeComponent();
28         }
29
30         
private string _getTabpage;
31
32         
public frmConnectCSDL(string _tabpage)
33             :
this()
34         {
35             _getTabpage = _tabpage;
36         }
37         
//--http://www.fourthbottle.com/2015/08/update-xml-node-value-using-csharp-.net.html
38         
private void btnThucHien_Click(object sender, EventArgs e)
39         {
40             
41             
string serverName = txtServerName.Text.Trim();
42             
string login = txtLogin.Text.Trim();
43             
string pass = txtPass.Text.Trim();
44             
string dataName = txtDatabase.Text.Trim();
45             
string name = txtName.Text.Trim();
46
47             
//int thuTuOTrong = 0;
48             
if (string.IsNullOrEmpty(serverName))
49             {
50                  txtServerName.Focus();
51                  
return;
52             }
53
54             
if (string.IsNullOrEmpty(login))
55             {
56                 txtLogin.Focus();
57                 
return;
58             }
59
60             
if (string.IsNullOrEmpty(pass))
61             {
62                 txtPass.Focus();
63                 
return;
64             }
65
66             
if (string.IsNullOrEmpty(dataName))
67             {
68                 txtDatabase.Focus();
69                 
return;
70             }
71
72             
if (string.IsNullOrEmpty(name))
73             {
74                 txtName.Focus();
75                 
return;
76             }
77
78                 
string startupPath = System.IO.Directory.GetCurrentDirectory().Replace("\\bin\\Release", "\\Resources").Replace("\\bin\\Debug", "\\Resources");
79                 XmlDocument doc =
new XmlDocument();
80                 doc.Load(startupPath +
"/ConnectCSDL.xml");
81                 XmlNodeList aDateNodes = doc.SelectNodes(
"/ConnectionString/SqlCon");
82
83                 
string AllSqlCon = "";
84
85                 AllSqlCon =
"server=" + serverName + "; uid=" + login + "; pwd=" + pass + ";database=" + dataName;
86
87                 
//chuoi ket noi csdl
88                 
foreach (XmlNode aDateNode in aDateNodes)
89                 {
90                     
//XmlAttribute DateAttribute = aDateNode.Attributes["SqlCon"];
91                     aDateNode.InnerText = AllSqlCon;
92                 }
93
94                 
//nhap name server de pm tu khoi dong
95                 XmlNodeList nameSv = doc.SelectNodes(
"/ConnectionString/NameSv");
96                 
foreach (XmlNode akey in nameSv)
97                 {
98                     akey.InnerText = txtName.Text.Trim();
99                 }
100
101                 
//nhap key su dung phan mem, key chon doi
102                 
//XmlNodeList keyBanQuyen = doc.SelectNodes("/ConnectionString/SerialNumber");
103                 
//foreach (XmlNode akey in keyBanQuyen)
104                 
//{
105                 
// akey.InnerText = txtValidateKey.Text.Trim();
106                 
//}
107
108                 doc.Save(startupPath +
"/ConnectCSDL.xml");
109
110                 
if (SoftQuanLyNhaHang.Models.connection.IsKet_Noi_CSDL() == true)
111                 {
112                     CauHinhHeThong.KET_NOI_CSDL =
true;
113
114
115                     txtKeyThongBao.Text =
"Kết nỗi thành công!";
116                     txtKeyThongBao.Visible =
true;
117
118                     
var t = new System.Windows.Forms.Timer();
119                     t.Interval =
3000; // it will Tick in 3 seconds
120                     t.Tick += (s, ex) =>
121                     {
122
123                         Application.Exit();
124                         t.Stop();
125                     };
126                     t.Start();
127                 }
128                 
else
129                 {
130                     txtKeyThongBao.Visible =
true;
131                     
return;
132                 }
133             
/*
134             }
135             
else
136             {
137                 //docten sql
138                 
string startupPath = System.IO.Directory.GetCurrentDirectory().Replace("\\bin\\Release", "\\Resources").Replace("\\bin\\Debug", "\\Resources");
139
140                 XmlDocument xmlDoc =
new XmlDocument();
141                 xmlDoc.Load(startupPath +
"/ConnectCSDL.xml");
142                 XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes(
"/ConnectionString");
143                 
string nameSv = string.Empty;
144
145                 
foreach (XmlNode node in nodeList)
146                 {
147                     nameSv = node.SelectSingleNode(
"NameSv").InnerText;
148                 }
149
150                 
if (!string.IsNullOrEmpty(nameSv))
151                 {
152                     //start sql service
153                     System.Diagnostics.Process process;
154                     System.Diagnostics.ProcessStartInfo startInfo;
155
156                     process =
new System.Diagnostics.Process();
157                     startInfo =
new System.Diagnostics.ProcessStartInfo();
158                    
159                     startInfo.Verb =
"runas";
160                     startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
161                     startInfo.FileName =
"cmd.exe";
162                     startInfo.Arguments =
"/C net start " + nameSv;
163                     process.StartInfo = startInfo;
164                     process.Start();
165                     process.WaitForExit();
166                 }
167
168                 txtKeyThongBao.Text =
"Kết nỗi thành công!";
169                 txtKeyThongBao.Visible =
true;
170                 
var t = new System.Windows.Forms.Timer();
171                 t.Interval =
3000; // it will Tick in 3 seconds
172                 t.Tick += (s, ex) =>
173                 {
174
175                     Application.Exit();
176                     t.Stop();
177                 };
178                 t.Start();
179             }
180              */

181             
//server=DESKTOP-VLEK6RU\SQL2008; uid=sa; pwd=1;database=QLBanHang
182         }
183
184
185         
private void frmConnectCSDL_Load(object sender, EventArgs e)
186         {
187             
if (_getTabpage == "KeyPm")
188             {
189                 tabControl1.TabPages.RemoveByKey(
"tabPageCsdl");
190
191                 txtSerialNumber.Text = CauHinhHeThong.getCPUID();
192
193                 
//-https://www.codeproject.com/Articles/38951/How-To-Hash-Data-Using-MD-and-SHA
194                 txtValidateKey.Text = CauHinhHeThong.GetSHA1HashData(txtSerialNumber.Text);
195                 
196             }
197             
else
198             {
199                 
//docten sql
200                 
string startupPath = System.IO.Directory.GetCurrentDirectory().Replace("\\bin\\Release", "\\Resources").Replace("\\bin\\Debug", "\\Resources");
201
202                 XmlDocument xmlDoc =
new XmlDocument();
203                 xmlDoc.Load(startupPath +
"/ConnectCSDL.xml");
204                 XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes(
"/ConnectionString");
205                 
string nameSv = string.Empty;
206
207                 
foreach (XmlNode node in nodeList)
208                 {
209                     nameSv = node.SelectSingleNode(
"NameSv").InnerText;
210                 }
211
212                 txtName.Text = nameSv;
213
214                 
//ten ket noi
215                 
string tenknoi = string.Empty;
216                 
foreach (XmlNode node in nodeList)
217                 {
218                     tenknoi = node.SelectSingleNode(
"SqlCon").InnerText;
219                 }
220
221                 
string[] arrtenkn = tenknoi.Split(';');
222                 
if (arrtenkn.Length > 0)
223                 {
224                     
//server=DESKTOP-VLEK6RU\SQL2008; uid=sa; pwd=1;database=QLBanHanxg
225                     
int demlap = 0;
226                     
foreach (string word in arrtenkn)
227                     {
228                         demlap +=
1;
229
230                         
switch (demlap)
231                         {
232                             
case 1:
233                                 txtServerName.Text = word.Replace(
"server=", "");
234                                 
break;
235                             
case 2:
236                                 txtLogin.Text = word.Replace(
"uid=", "");
237                                 
break;
238                             
case 3:
239                                 txtPass.Text = word.Replace(
"pwd=", "");
240                                 
break;
241                             
case 4:
242                                 txtDatabase.Text = word.Replace(
"database=", "");
243                                 
break;
244                         }
245                         
246                     }
247                 }
248
249                 tabControl1.TabPages.RemoveByKey(
"tabPageKeyPm");
250             }
251
252             
253
254         }
255
256         
private void btnDong_Click(object sender, EventArgs e)
257         {
258             Application.Exit();
259         }
260
261         
private void btnGetKeyValidate_Click(object sender, EventArgs e)
262         {
263             
264
265
266             
//-https://www.codeproject.com/Articles/38951/How-To-Hash-Data-Using-MD-and-SHA
267             
//waleed
268             
//txtValidateKey.Text = CauHinhHeThong.GetSHA1HashData(txtPassSerial.Text.Trim());
269
270         }
271
272         
private void btnGetKey_Click(object sender, EventArgs e)
273         {
274
275             DateTime ngayBatD = DateTime.Now;
276             ngayBatD = ngayBatD.AddDays(+
31);
277
278             
if (txtPassSerial.Text.Trim() == "top10pm6868")
279             {
280                 txtValidateKey.Text = CauHinhHeThong.GetSHA1HashData(txtSerialNumber.Text.Trim() + txtPassSerial.Text.Trim());
281             }
282
283             
if (txtPassSerial.Text.Trim() == "demo6868")
284             {
285                 txtValidateKey.Text = CauHinhHeThong.GetSHA1HashData(txtSerialNumber.Text.Trim() + txtPassSerial.Text.Trim() + CauHinhHeThong.Format_HienThiNgayThangNam(ngayBatD.ToString(),
false));
286             }
287
288         }
289
290         
private void button2_Click(object sender, EventArgs e)
291         {
292             Application.Exit();
293         }
294
295         
private void button1_Click(object sender, EventArgs e)
296         {
297             
//1813121441931318089196128241523121140163250248149
298             
string startupPath = System.IO.Directory.GetCurrentDirectory().Replace("\\bin\\Release", "\\Resources").Replace("\\bin\\Debug", "\\Resources");
299             XmlDocument doc =
new XmlDocument();
300             doc.Load(startupPath +
"/ConnectCSDL.xml");
301
302             
//nhap key su dung phan mem, key chon doi
303             XmlNodeList keyBanQuyen = doc.SelectNodes(
"/ConnectionString/SerialNumber");
304             
foreach (XmlNode akey in keyBanQuyen)
305             {
306                 akey.InnerText = txtValidateKey.Text.Trim();
307             }
308
309             doc.Save(startupPath +
"/ConnectCSDL.xml");
310
311             
if (SoftQuanLyNhaHang.Models.connection.IsKet_Noi_CSDL() == true)
312             {
313                 CauHinhHeThong.KET_NOI_CSDL =
true;
314
315                 txtKeyThongBao.Text =
"Kết nỗi thành công!";
316                 txtKeyThongBao.Visible =
true;
317
318                 
var t = new System.Windows.Forms.Timer();
319                 t.Interval =
3000; // it will Tick in 3 seconds
320                 t.Tick += (s, ex) =>
321                 {
322
323                     Application.Exit();
324                     t.Stop();
325                 };
326                 t.Start();
327             }
328             
else
329             {
330                 txtKeyThongBao.Visible =
true;
331                 
return;
332             }
333         }
334     }
335 }


Gõ tìm kiếm nhanh...